import { cloneDeep } from "lodash";
import { HttpClient } from "../service/Http/HttpClient";
import { Modal } from "antd";
export const getUserInfo = (attr: string) => {
  let str: string | null = sessionStorage.getItem("userInfo");
  if (str) {
    let Json = JSON.parse(str);
    if (Json[attr]) {
      return Json[attr];
    } else {
      return null;
    }
  } else {
    return null;
  }
};
//直接转化成antdTable能识别的树型数据
export const listToAntdTableTreeData = (list: any[]) => {
  return ergodicTree(listToTree(ergodicListAddKey(list)));
};
//直接转化成antd TreeSelect能识别的树型数据
export const listToAntdTreeSelectTreeData = (list: any[]) => {
  return ergodicTree(listToTree(ergodicListAddKey(ergodicListAddValue(list))));
};
//直接转化成antd Tree能识别的树型数据
export const formattingTreeSelectTreeData = (list: any[]) => {
  return ergodicTree(listToTree2(list));
};
export const getFormData = (pream: any) => {
  let formData = new FormData();
  for (let key in pream) {
    formData.append(key, pream[key]);
  }
  return formData;
};
/**去掉所有空 children*/
export const ergodicTree = (tree: any) => {
  let arr = tree.map((item) => {
    if (item.children && item.children.length) {
      item.children = ergodicTree(item.children);
    } else {
      delete item.children;
    }
    return item;
  });
  return arr;
};
/** 给list结构添加key*/
export const ergodicListAddIdToKey = (list: any[]) => {
  let arrLi: any = [];
  arrLi = list.map((item) => {
    let ai = cloneDeep(item);
    ai.key = item.id;
    return ai;
  });
  return arrLi;
};
/** 给list结构添加key，parentkey*/
export const ergodicListAddKey = (list: any[]) => {
  let arrLi: any = [];
  arrLi = list.map((item) => {
    let ai = cloneDeep(item);
    ai.key = item.treeId;
    ai.parentkey = item.parentId;
    return ai;
  });

  return arrLi;
};
/** 给list结构添加title，value*/
export const ergodicListAddValue = (list: any[]) => {
  let arrLi: any = [];
  arrLi = list.map((item) => {
    let ai = cloneDeep(item);
    ai.value = item.treeId;
    if (item.orgName) {
      ai.title = item.orgName;
    }
    if (item.name) {
      ai.title = item.name;
    }
    return ai;
  });
  // for (let i = 0; i < list.length; i++) {
  //   list[i].key = list[i].treeId;
  //   list[i].parentkey = list[i].parentId;
  // }
  return arrLi;
};
/**list转树结构。 */
export const listToTree = (list: any) => {
  if (!Array.isArray(list) || list.length <= 1) return list;
  let map: any = {},
    roots: any = [];
  for (let i = 0; i < list.length; i++) {
    map[list[i].key] = i;
    list[i].children = [];
  }
  for (let i = 0; i < list.length; i++) {
    let node = list[i];
    if (node.parentkey === null) {
      roots.push(node);
    } else {
      list[map[node.parentkey]].children.push(node);
    }
  }
  console.log(roots);
  return roots;
};
//新list转树结构
export const listToTree2 = (list: any) => {
  if (!Array.isArray(list) || list.length <= 1) return list;
  let map: any = {},
    roots: any = [];
  for (let i = 0; i < list.length; i++) {
    map[list[i].id] = i;
    list[i].children = [];
  }
  for (let i = 0; i < list.length; i++) {
    let node = list[i];
    if (node.pId === null) {
      roots.push(node);
    } else {
      list[map[node.pId]].children.push(node);
    }
  }
  return roots;
};
export const listAddTreeId = (list: any) => {
  if (!Array.isArray(list) || list.length <= 1) return list;
  list = list.map((item) => {
    item.treeId = item.id;
    return item;
  });
  return list;
};
//根据值删除数组的某项
export const arrRemoveItem = (val: any, arr: any[]) => {
  var index = arr.indexOf(val);
  if (index > -1) {
    arr.splice(index, 1);
  }
  return arr;
};
//根据值删除数组的某项
export const arrRemoveItem2 = (val: any, arr: any[]) => {
  let index: number = -1;
  arr.map((item: any, i: number) => {
    if (item.id === val) {
      index = i;
    }
    return item;
  });
  if (index > -1) {
    arr.splice(index, 1);
  }
  return arr;
  // console.log(val, arr, index);
};
export const uniqueArr = (arr1, arr2) => {
  //合并两个数组
  arr1.push(...arr2); //或者arr1 = [...arr1,...arr2]
  //去重
  let arr3 = Array.from(new Set(arr1)); //let arr3 = [...new Set(arr1)]
  return arr3;
};

/**
 * 检查是否为空对象
 * @param {Object} obj 要检查的对象
 * @return {Boolean}
 **/
export const __isEmptyObject = (obj) => {
  for (let key in obj) {
    console.log(key);
    return false;
  }
  return true;
};

//查询数据
export const getNetWorkData = (
  url: string,
  param: any,
  caller: any,
  callback: Function,
  errFuc?: Function
) => {
  const http = HttpClient.instance();
  let initData = { method: "POST", headers: {}, body: JSON.stringify(param) };
  http
    .fetch(url, initData)
    .then((result: any) => {
      if (result.success) {
        callback.call(caller, result);
      }
    })
    .catch((err) => {
      Modal.error({
        title: "系统错误，请稍后再试！",
        onOk: () => {
          if (errFuc) {
            errFuc.call(caller, err);
          }
        },
      });
    });
};

//导出文件
export const exportFile = (
  url: string,
  param: any,
  fileName: string,
  caller?: any,
  errFuc?: Function
) => {
  const http = HttpClient.instance();
  let initData = {
    method: "POST",
    headers: { qingqiuType: "file" },
    body: JSON.stringify(param),
  };
  http
    .fetch(url, initData)
    .then((result) => {
      const link = document.createElement("a");
      link.style.display = "none";
      link.href = URL.createObjectURL(result);
      link.download = fileName;
      document.body.appendChild(link);
      link.click();
      // 释放的 URL 对象以及移除 a 标签
      URL.revokeObjectURL(link.href);
      document.body.removeChild(link);
    })
    .catch((err) => {
      Modal.error({
        title: "系统错误，请稍后再试！",
        onOk: () => {
          if (errFuc) {
            errFuc.call(caller, err);
          }
        },
      });
    });
};

//导入文件
export const importFile = (
  url: string,
  formData: FormData,
  caller: any,
  callback: Function,
  errFuc?: Function
) => {
  const http = HttpClient.instance();
  let initData = { method: "POST", body: formData };
  http
    .fetch(url, initData)
    .then((result: any) => {
      if (result.success) {
        callback.call(caller, result);
      }
    })
    .catch((err) => {
      Modal.error({
        title: "系统错误，请稍后再试！",
        onOk: () => {
          if (errFuc) {
            errFuc.call(caller, err);
          }
        },
      });
    });
};
